<?php
    // 接收数据
    $price = $_REQUEST["price"];
    $goodsMsg = $_REQUEST["goodsMsg"];
    $num = $_REQUEST["num"];
    $user = $_REQUEST["user"];
    $img = $_REQUEST["img"];
    $goodsid = $_REQUEST["goodsid"];
    $shop = $_REQUEST["shop"];

    $link = @mysqli_connect("localhost:3306","root","root","xmyp");

    if(!$link){
        $data = array("code"=>0,"errMsg"=>"数据路选择失败","sucMsg"=>"");
        die(json_encode($data));
    }

    $sql = "INSERT cart (user,goodsid,price,goodsMsg,num,shop,img) VALUES('".$user."','".$goodsid."','".$price."','".$goodsMsg."','".$num."','".$shop."','".$img."')";

    $res = mysqli_query($link, $sql);

    if($res){
        echo json_encode(select());
    }else{
        $data = array("code"=>1,"errMsg"=>"插入失败","sucMsg"=>"");
        echo json_encode($data);
    }

    function select(){
        // 将$link提前处理
        global $link;
        $sql = "SELECT * FROM cart";
        $res = mysqli_query($link, $sql);
        if($res){
            $bigArr = array();
            while(true){
                $arr = mysqli_fetch_assoc($res);
                if(!$arr){
                   break;
                }
                array_push($bigArr, $arr);
            }
            $data = array("code"=>2,"errMsg"=>"","sucMsg"=>$bigArr);

        }else{
            // 如果失败，返回失败的数据信息
            $data = array("code"=>1,"errMsg"=>"请求失败","sucMsg"=>"");
        }
        return $data;
    }
